package org.indp.mongowl.ui;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;
import org.indp.mongowl.model.ConnectionProfile;
import org.indp.mongowl.services.ConnectionManager;
import org.indp.mongowl.services.ServerConnection;

import java.util.List;

/**
 *
 */
public class ConnectionSelectorComponent extends Panel {

    public static void setup(UI ui) {
        ConnectionSelectorComponent component = new ConnectionSelectorComponent();
        VerticalLayout root = new VerticalLayout(component);
        root.setComponentAlignment(component, Alignment.MIDDLE_CENTER);
        root.setSizeFull();
        ui.setContent(root);
        ui.getPage().setTitle(MongowlUI.DEFAULT_TITLE);
    }

    public ConnectionSelectorComponent() {
        setSizeUndefined();
        setStyleName(Reindeer.PANEL_LIGHT);
        setContent(createProfileLinks());
    }

    private Component createProfileLinks() {
        VerticalLayout vl = new VerticalLayout();
        vl.setSizeUndefined();
        List<ConnectionProfile> profiles = ConnectionManager.getConnectionProfiles();
        for (final ConnectionProfile profile : profiles) {
            Button button = new Button(profile.getName(), new Button.ClickListener() {
                @Override
                public void buttonClick(Button.ClickEvent clickEvent) {
                    Component content;
                    try {
                        content = createParametersComponent(profile);
                    } catch (Exception e) {
                        Notification.show("Connection failed\n", e.getMessage(), Notification.Type.ERROR_MESSAGE);
                        return;
                    }
                    setContent(content);
                }
            });
            vl.addComponent(button);
        }
        return vl;
    }

    private Component createParametersComponent(ConnectionProfile profile) throws Exception {
        final ServerConnection serverConnection = ConnectionManager.getServerConnection(profile);
        VerticalLayout vl = new VerticalLayout();
        List<String> databaseNames = serverConnection.getMongoClient().getDatabaseNames();
        for (final String databaseName : databaseNames) {
            vl.addComponent(new Button(databaseName, new Button.ClickListener() {
                @Override
                public void buttonClick(Button.ClickEvent clickEvent) {
                    DatabaseWorkspaceComponent.setup(getUI(), serverConnection, databaseName);
                }
            }));
        }
        vl.addComponent(new Button("<< Back", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent clickEvent) {
                setContent(createProfileLinks());
            }
        }));
        return vl;
    }
}
